package com.zbkj.crmeb.store.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.taobao.pac.sdk.cp.PacClient;
import com.taobao.pac.sdk.cp.SendSysParams;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.CustomsDeclareInfo;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.DeclareInfo;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.GlobalSaleOrderNotifyRequest;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.OrderAmountInfo;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.OrderItem;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.ReceiverInfo;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.RefunderInfo;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.SenderInfo;
import com.taobao.pac.sdk.cp.dataobject.response.GLOBAL_SALE_ORDER_NOTIFY.GlobalSaleOrderNotifyResponse;
import com.zbkj.crmeb.store.model.StoreOrder;
import com.zbkj.crmeb.store.service.CainiaoStoreOrderService;
import com.zbkj.crmeb.store.vo.StoreOrderInfoVo;

import lombok.extern.slf4j.Slf4j;

/**
 * 菜鸟中心仓订单
 * @author daigb
 * @date:  2023-02-02 15:14
 */
@Slf4j
@Service
public class CainiaoStoreOrderServiceImpl implements CainiaoStoreOrderService {
	private static String appkey = "795670";
	private static String secretKey = "3705lcoVJp70304mlz3v8517584RW07F";
	private static String url = "http://link.cainiao.com/gateway/link.do"; // 生产
//	private static String url = "https://prelink.cainiao.com/gateway/link.do"; //联调
	
	/**
	 * 订单推送
	 */
    @Override
    public GlobalSaleOrderNotifyResponse pushOrder(StoreOrder storeOrder, List<StoreOrderInfoVo> infos) {
		try {
			// 请求
			GlobalSaleOrderNotifyRequest globalSaleOrderNotifyRequest = new GlobalSaleOrderNotifyRequest();
			globalSaleOrderNotifyRequest.setOwnerUserId("2212715138606"); // 货主id
			globalSaleOrderNotifyRequest.setExternalOrderCode(storeOrder.getOrderId()); // ERP订单编码
			globalSaleOrderNotifyRequest.setOrderType("OPEN_COLLECT"); // 订单跨境业务类型
//			globalSaleOrderNotifyRequest.setStoreCode("TRAN_STORE_30442235"); // 仓储编码，ERP指定仓库发货时需要传值，编码由菜鸟提供。如不传值则由菜鸟指定仓库发货
			globalSaleOrderNotifyRequest.setStoreCode("TRAN_STORE_30228532"); // 香港仓编码
//			globalSaleOrderNotifyRequest.setExternalShopName("意大利麦吉集货仓"); // 店铺名称
			globalSaleOrderNotifyRequest.setExternalShopName("cn-lavita供货商"); // 香港仓店铺名称
			globalSaleOrderNotifyRequest.setOrderSource("689"); // 渠道编码
			globalSaleOrderNotifyRequest.setOrderCreateTime(storeOrder.getCreateTime()); // 订单创建时间
			globalSaleOrderNotifyRequest.setOrderPayTime(storeOrder.getPayTime()); // 订单支付时间
			globalSaleOrderNotifyRequest.setSaleMode("1"); // 销售模式0-线下 1-线上
			globalSaleOrderNotifyRequest.setServiceId("100100"); // 服务id
			// 收货方信息
			ReceiverInfo receiverInfo = new ReceiverInfo();
			receiverInfo.setCountry("CN"); // 国家
			receiverInfo.setProvince(storeOrder.getProvince()); // 省份
			receiverInfo.setCity(storeOrder.getCity()); // 城市
			receiverInfo.setDistrict(storeOrder.getDistrict()); // 区县
			receiverInfo.setTown(""); // 镇/街道(非必填)
			receiverInfo.setAddress(storeOrder.getDetail()); // 详细地址
			receiverInfo.setName(storeOrder.getRealName()); // 姓名
			receiverInfo.setContactNo(storeOrder.getUserPhone().contains(" ") ? storeOrder.getUserPhone().split(" ")[1] : storeOrder.getUserPhone()); // 电话
			globalSaleOrderNotifyRequest.setReceiverInfo(receiverInfo);
//			// 发件人信息
//			SenderInfo senderInfo = new SenderInfo();
//			senderInfo.setCountry("IT"); // 国家
//			senderInfo.setProvince("Italy"); // 省份
//			senderInfo.setCity("Segrate Milano"); // 城市
//			senderInfo.setDistrict(""); // 区县
//			senderInfo.setTown(""); // 镇/街道(非必填)
//			senderInfo.setAddress("Via Londra 1 BETA Trans Cargo Village"); // 详细地址
//			senderInfo.setName("FAN JIAQI"); // 姓名
//			senderInfo.setContactNo("+33 7 87 80 49 32"); // 电话
//			globalSaleOrderNotifyRequest.setSenderInfo(senderInfo);
//			// 退货人信息
//			RefunderInfo refunderInfo = new RefunderInfo();
//			refunderInfo.setCountry("IT"); // 国家
//			refunderInfo.setProvince("Italy"); // 省份
//			refunderInfo.setCity("Segrate Milano"); // 城市
//			refunderInfo.setDistrict(""); // 区县
//			refunderInfo.setTown(""); // 镇/街道(非必填)
//			refunderInfo.setAddress("Via Londra 1 BETA Trans Cargo Village"); // 详细地址
//			refunderInfo.setName("FAN JIAQI"); // 姓名
//			refunderInfo.setContactNo("+33 7 87 80 49 32"); // 电话
//			globalSaleOrderNotifyRequest.setRefunderInfo(refunderInfo);

			// 发件人信息
			SenderInfo senderInfo = new SenderInfo();
			senderInfo.setCountry("CN"); // 国家
			senderInfo.setProvince("香港特别行政区"); // 省份
			senderInfo.setCity("新界"); // 城市
			senderInfo.setDistrict("北区"); // 区县
			senderInfo.setTown(""); // 镇/街道(非必填)
			senderInfo.setAddress("香港新界上水剑桥A座 311"); // 详细地址
			senderInfo.setName("胡海燕"); // 姓名
			senderInfo.setContactNo("18966273219"); // 电话
			globalSaleOrderNotifyRequest.setSenderInfo(senderInfo);
			// 退货人信息
			RefunderInfo refunderInfo = new RefunderInfo();
			refunderInfo.setCountry("CN"); // 国家
			refunderInfo.setProvince("香港特别行政区"); // 省份
			refunderInfo.setCity("新界"); // 城市
			refunderInfo.setDistrict("北区"); // 区县
			refunderInfo.setTown(""); // 镇/街道(非必填)
			refunderInfo.setAddress("香港新界上水剑桥A座 311"); // 详细地址
			refunderInfo.setName("胡海燕"); // 姓名
			refunderInfo.setContactNo("18966273219"); // 电话
			globalSaleOrderNotifyRequest.setRefunderInfo(refunderInfo);

			// 订单商品信息
			List<OrderItem> orderItemList = new ArrayList<>();
			BigDecimal actualPayment = BigDecimal.ZERO;
			for(StoreOrderInfoVo info : infos) {
				OrderItem item = new OrderItem();
				item.setItemId(info.getInfo().getProductInfo().getBarCode()); // 菜鸟货品ID
				item.setItemQuantity(info.getInfo().getCartNum()); // 商品数量
				// 附加信息
				DeclareInfo declareInfo = new DeclareInfo();
				declareInfo.setConsumptionTax(0L); // 消费税金额
				declareInfo.setCustomsTax(0L); // 商品关税
				declareInfo.setTotalTax(0L); // 商品综合税
				declareInfo.setVat(0L); // 增值税
				declareInfo.setItemTotalPrice(info.getPayPrice().movePointRight(2).longValue()); // 商品总价优惠前
				declareInfo.setItemTotalActualPrice(info.getPayPrice().movePointRight(2).longValue()); // 商品总价优惠后
				actualPayment = actualPayment.add(info.getPayPrice());
				item.setDeclareInfo(declareInfo);
				
				orderItemList.add(item);
			}
			globalSaleOrderNotifyRequest.setOrderItemList(orderItemList);
			
			// 附加参数
			OrderAmountInfo orderAmountInfo = new OrderAmountInfo();
			orderAmountInfo.setCoupon(0L);
			orderAmountInfo.setConsumptionTax(0L); // 消费税金额
			orderAmountInfo.setPostFee(0L); // 运费
			orderAmountInfo.setVat(0L); // 增值税金额
			orderAmountInfo.setCustomsTax(0L); // 关税金额
			orderAmountInfo.setActualPayment(actualPayment.movePointRight(2).longValue()); // 实付金额
			orderAmountInfo.setTotalTax(0L); // 综合税金额
			orderAmountInfo.setInsurance(0L); // 保险费金额
			orderAmountInfo.setCurrency("CNY"); // 支付币种
			orderAmountInfo.setDutiablePrice(actualPayment.movePointRight(2).longValue()); // 订单完税总价
			globalSaleOrderNotifyRequest.setOrderAmountInfo(orderAmountInfo);
			CustomsDeclareInfo customsDeclareInfo = new CustomsDeclareInfo();
			customsDeclareInfo.setPayOrderId(storeOrder.getOutOrderId()); // 支付单号
			String payChannel = storeOrder.getPayType().equals("weixin") ? "WEIXINPAY" : "HUIFU";
			customsDeclareInfo.setPayChannel(payChannel); // 支付渠道
			customsDeclareInfo.setBuyerName(storeOrder.getRealName()); // 姓名
			customsDeclareInfo.setBuyerIDType("1"); // 证件类型
			customsDeclareInfo.setBuyerIDNo(storeOrder.getIdCard()); // 身份证
			customsDeclareInfo.setBuyerPlatformId(storeOrder.getRealName()); // 和buyerName一致
			globalSaleOrderNotifyRequest.setCustomsDeclareInfo(customsDeclareInfo);

			// 初始化
			PacClient pacClient = new PacClient(appkey, secretKey, url);
			pacClient.getLogger().setLogEnable(false); // 关闭日志打印
			// 发送请求
			SendSysParams sendSysParams = new SendSysParams();
			sendSysParams.setFromCode("d6c49d19a3aa64508bc9a7e5fc392711");
			GlobalSaleOrderNotifyResponse response = pacClient.send(globalSaleOrderNotifyRequest, sendSysParams);

			log.info("--------------- 菜鸟中心仓订单推送结果: {} ---------------", JSON.toJSONString(response));
			log.info("--------------- 菜鸟中心仓订单推送结果isSuccess: {} ---------------", response.isSuccess());
			return response;
		} catch (Exception e) {
			e.printStackTrace();
		}
		GlobalSaleOrderNotifyResponse response = new GlobalSaleOrderNotifyResponse();
		response.setSuccess(false);
		return response;
	}
    
}
